# https://blog.csdn.net/sunny2038/article/details/9202641
import cv2

ifile = 'tmp/noised_umbr_gaussian.jpg'
img = cv2.imread(ifile, cv2.IMREAD_GRAYSCALE)


def orgcanny():
    gkim = cv2.GaussianBlur(img, (3, 3), 0)
    canny = cv2.Canny(gkim, 65, 150)

    cv2.imshow('Canny', canny)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


def dycanny():

    import numpy as np

    def CannyThreshold(lowThreshold):
        detected_edges = cv2.GaussianBlur(img, (3, 3), 0)
        detected_edges = cv2.Canny(
            detected_edges, lowThreshold, lowThreshold * ratio, apertureSize=kernel_size)
        # just add some colours to edges from original image.
        dst = cv2.bitwise_and(img, img, mask=detected_edges)
        cv2.imshow('canny demo', dst)

    lowThreshold = 0
    max_lowThreshold = 100
    ratio = 3
    kernel_size = 3

    cv2.namedWindow('canny demo')

    cv2.createTrackbar('Min threshold', 'canny demo',
                       lowThreshold, max_lowThreshold, CannyThreshold)

    CannyThreshold(0)  # initialization
    if cv2.waitKey(0) == 27:
        cv2.destroyAllWindows()


orgcanny()
